//
//  AlertSeleView.m
//  CYWFrame
//
//  Created by Macx on 2017/12/11.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AlertSeleView.h"
#import "UIView+LZCView.h"
#import "Share.h"
#import "LCButton.h"

@interface AlertSeleView ()

@property (nonatomic, strong) UIView *alertview;
@property (nonatomic, assign) BOOL judge;

@property (nonatomic, strong) NSDictionary *param;

@property (nonatomic, strong) UILabel *label;
//@property (nonatomic, strong) NSMutableString *str;

@end

@implementation AlertSeleView {
    CGFloat seleW;
    CGFloat seleW2;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        //创建遮罩
        UIView *mask = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.width, self.height)];
        [self addSubview:mask];
        mask.backgroundColor = [UIColor blackColor];
        mask.alpha = 0.3f;
        [mask addTarget:self action:@selector(maskClick)];
        
        //创建alert
        self.alertview = [[UIView alloc]initWithFrame:CGRectMake(10, 0, SCREEN_WIDTH-20, SW(4))];
        self.alertview.centerY = mask.centerY;
        self.alertview.backgroundColor = [UIColor colorWithRed:255 green:255 blue:255 alpha:0.5];
        [self addSubview:_alertview];
        [self exChangeOut:self.alertview dur:0.2];
    }
    return self;
}
- (void)layoutSubviews
{
    [super layoutSubviews];
    
    // 第二次就不需要重新创建
    if (self.judge == 0) {
        
        NSArray *arr = @[@"专家", @"所有人"];
        for (int i = 0; i < arr.count; i++) {
            UIButton *l = [UIButton buttonWithType:UIButtonTypeCustom];
            [self.alertview addSubview:l];
            [l setTitle:arr[i] forState:UIControlStateNormal];
            [l setTitleColor:UIMainColorRGB forState:UIControlStateNormal];
            l.titleLabel.font = kUHSystemFontWithSize(15);
            l.frame = CGRectMake(0, self.alertview.height/2*i, self.alertview.width, self.alertview.height/2);
            
            if (i == 0) {
                UIView *v = [[UIView alloc] initWithFrame:CGRectMake(20, l.height-1, self.alertview.width-40, 1)];
                [self.alertview addSubview:v];
                v.backgroundColor = UIMainColorRGB;
            }
            
            [l addTarget:self action:@selector(click:) forControlEvents:UIControlEventTouchUpInside];
            
            l.tag = 100+i;
        }
    }
    self.judge = YES;
    
}
- (void)click:(UIButton *)btn {
    if (btn.tag == 100) {
        self.label.text = @"专家";
//        self.str = @"pro";
    }
    else
    {
        self.label.text = @"所有人";
//        self.str = @"all";
    }
    
    [self maskClick];
}

#pragma mark ==========响应方法==========
// 弹出
-(void)exChangeOut:(UIView *)changeOutView dur:(CFTimeInterval)dur{
    
    CAKeyframeAnimation * animation;
    animation = [CAKeyframeAnimation animationWithKeyPath:@"transform"];
    animation.duration = dur;
    animation.removedOnCompletion = NO;
    animation.fillMode = kCAFillModeForwards;
    NSMutableArray *values = [NSMutableArray array];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(0.1, 0.1, 1.0)]];
    [values addObject:[NSValue valueWithCATransform3D:CATransform3DMakeScale(1.0, 1.0, 1.0)]];
    animation.values = values;
    animation.timingFunction = [CAMediaTimingFunction functionWithName: @"easeInEaseOut"];
    [changeOutView.layer addAnimation:animation forKey:nil];

}
- (void)maskClick
{
    // 取消弹框
    [UIView animateWithDuration:0.2 animations:^{
        self.alpha = 0.0;
    } completion:^(BOOL finished) {
        self.alertview = nil;
        [self removeFromSuperview];
    }];

}

- (void)initShow:(UILabel *)label {
    self.label = label;
}

//- (void)initShow:(NSDictionary *)param {
//    self.param = param;
//}

/*
 // Only override drawRect: if you perform custom drawing.
 // An empty implementation adversely affects performance during animation.
 - (void)drawRect:(CGRect)rect {
 // Drawing code
 }
 */

@end


